#!/bin/sh
# Copyright (c) <2002>, Intel Corporation
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or 
# without modification, are permitted provided that the following 
# conditions are met:
# 
# Redistributions of source code must retain the above copyright 
# notice, this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright 
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the distribution.
# 
# Neither the name of Intel Corporation, nor the names 
# of its contributors may be used to endorse or promote products 
# derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $Id: tvs2xml,v 1.7 2004/07/21 16:43:31 xling Exp $

usage()
{
	cat << EOF
	Usage: tvs2xml <definitionfile> [inputfile] [outputfile] [errfile] [screenlogfile]
	inputfile:
		Summary Report file generated by TVS
	definitionfile:
		each file defined by suites maintainer
	errfile:
		Full Report file generated by TVS
EOF
	exit $1
}

if [ $# -eq 0 ] || [ $# -gt 5 ];then
	usage 1  1>&2 
fi
definefile=$1
inputfile=${2:-"/dev/null"}
outputfile=${3:-"results.xml"}
errfile=$4
screenlog=$5
echo "<?xml version=\"1.0\" ?>" > $outputfile
echo "<TestResults>" >> $outputfile
awk '
function checkendfeature()
{
	if(beginfeature==1)
	{	
		print "</Feature>"
		beginfeature=0
	}
	
}
BEGIN {FS=":" ;
	req_total=0;
	req_pass=0;
	req_fail=0;
	time=1
	beginfeature=0
	}
	/^#/ {next}
	/^$/ {next}
	/^\[/ {next}
	/^RTitle/ {
		  rtitle=$2
		  print "<RTitle>" 
		  printf("%s\n",rtitle) 
		  print "</RTitle>" 
	}
	/^ReqId/ {
		  reqid=$2
		  printf("<Requirement ReqID=\"%s\">\n",reqid); 
	}
	/^RDesc/{
		des=1
	}
	/^EndRDesc/{
		des=0
		print "<RDesc>"
		for(i=1;i<=line;i++)
		{
			print descr[i] 	
		}
		print "</RDesc>"
	}
	/^SubReq/ {
		checkendfeature()
		if (time > 1)
		{
			print "</SubReq>" 
		}
		stitle=$2
		sid=$3
		printf("<SubReq STitle=\"%s\" SRID=\"%s\">\n",stitle,sid) 
		time++
	}
	/^Feature/{
		checkendfeature()
		beginfeature=1
		fname=$2
		if(NF>=3 && $3 != "") {
		scen=$3
		v=split(scen,names," ")
		total=0
		pass=0
		fail=0
		for (i=1;i<=v;i++)
		{
			name=names[i];
			cmd=sprintf("cat %s|grep -w %s",inputfile,name)
			if(cmd|getline)
			{
				split($0,text," ")
				total+=text[2];
				pass+=text[3];
				fail+=text[4];
				req_total+=total;
				req_pass+=pass;
				req_fail+=fail;
			}
		}
		others=total-(pass+fail)
		printf("<Feature Name=\"%s\" Total=\"%d\" Pass=\"%d\" Fail=\"%d\" Other=\"%d\">\n",fname,total,pass,fail,others) 
		}else{
		printf("<Feature Name=\"%s\">\n" ,fname) 
		}
	}
	/^Data/ {
		find=1
	}
	/^EndData/ {
		find=0
		print "<Data>"
		for(i=1;i<=count;i++)
		{
			print data[i] 	
		}
		print "</Data>"
		checkendfeature()
	}
	{
		if(des==1){
			descr[line]=$0
			line++
		}
		if(find==1){
			data[count]=$0
			count++
		}
	}
END{
	checkendfeature()
	print "</SubReq>"
	print "<data>"
#	printf("Total_cases=\"%d\" Total_Pass=\"%d\" Total_Fail=\"%d\" Other=\"%d\">\n",req_total,req_pass,req_fail,req_total-(req_pass+req_fail)) 
	if(i>count)
	{
		printf("<![CDATA[")
		while((getline < errfile)>0)
			print	
		print "================================================";
		while((getline < screenlog)>0)
			print
		printf("]]>")
	}

	print "</data>"
	print "</Requirement>"  
	print "</TestResults>" 
}
' inputfile=$inputfile  errfile=$errfile screenlog=$screenlog $definefile |col -b >> $outputfile 

